function spmk_get_suv(nii_file)
%% get suv from dicom
% FORMAT spmk_get_suv(nii_file)

if nargin ==0 
    if isempty(dir('*.nii'))
        system(['D:\MRI\MRIcron\Resources\dcm2niix.exe ',pwd]);
    end
    nii_files = dir('*.nii');
    nii_file = nii_files(1).name;
end

v_raw = spm_vol(nii_file);

y = spm_read_vols(v_raw);
dcms = dir('*.dcm');
if length(dcms)==0
    dcms = dir('00*');
end
    % check if the nifti dim equals to the file counts 
if v_raw.dim(3)~=length(dcms)
    disp('wrong dim')
    return
end
% main loop
for m =1:length(dcms)
    dcm = dcms(m).name;
    header = dicominfo(dcm);
    x = double(dicomread(dcm));
    RescaleSlope = header.RescaleSlope;
    weight = header.PatientWeight;
    tot_dose = header.RadiopharmaceuticalInformationSequence.Item_1.RadionuclideTotalDose;
    half_life = header.RadiopharmaceuticalInformationSequence.Item_1.RadionuclideHalfLife; % [second]
    
    acq_time = datetime(header.AcquisitionTime(1:6),'InputFormat','HHmmss');
    inj_time = datetime(header.RadiopharmaceuticalInformationSequence.Item_1.RadiopharmaceuticalStartDatetime(9:14),'InputFormat','HHmmss');
    delta_time = seconds(diff([inj_time acq_time]));% [second]
    
    corrected_dose =tot_dose  * 2^(- delta_time  / half_life); % [Bq]
    SUV_coef = RescaleSlope * weight * 1000/ corrected_dose;
    % [g/Bq] = [] * [Kg]* 1000[g/kg] / [Bq]
    
    %     delta_time = (str2double(header.AcquisitionTime) - str2double(header.RadiopharmaceuticalInformationSequence.Item_1.RadiopharmaceuticalStartDatetime)) / 100; % [min]

    y(:,:,m) =rot90(x,3) * SUV_coef  ; %[g/ml] = [Bq/ml] * [g/Bq]
end
  
    % Create SUV image
    v_out=v_raw;
    v_out.fname = ['SUV_',v_raw.fname];
    v_out.dt = [64 0];
    v_out.dim
    spm_write_vol(v_out, y)
    delete(nii_file)
    % open the file
%     winopen(v_out.fname)
end
